package com.github.tzraeq.d2d.listener;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.github.tzraeq.d2d.RND2dModule;

import cn.anxin.a9.libice.IceTransListener;

public class D2dTransListener implements IceTransListener {

    private ReactContext reactContext;
    private Integer id;

    // private final RCTEventEmitter eventEmitter;

    public D2dTransListener(ReactContext reactContext, Integer id){
        this.reactContext = reactContext;
        this.id = id;
        // this.eventEmitter = reactContext.getJSModule(RCTEventEmitter.class);
    }

    @Override
    public void onComplete(int op, int status) {
        WritableMap event = Arguments.createMap();
        event.putInt("op",op);
        event.putInt("status",status);
        reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(RND2dModule.RND2D_ON_COMPLETE + "-" + this.id,event);
    }

    @Override
    public void onRxData(int op, String addr, int type, byte[] data) {
        WritableMap event = Arguments.createMap();
        event.putInt("op",op);
        event.putString("addr",addr);
        event.putInt("type",type);
        reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(RND2dModule.RND2D_ON_DATA + "-" + this.id,event);
    }
}
